import {
	addQuotation,
	getLocalFeeDetail,
	getQuotationDetail,
	getQuotationListForPage,
	getRecordById,
	getTotalAmoumt,
	updateQuotationv
} from "../../services/quotation/quotationFee";
import {message} from "antd";
import Constants from '../../utils/Constants'
const R = require('ramda');

export default {
	namespace: 'quotationFee',
	state: {
		page: {},
		list: [],
		localFeeList: [],
		declareFeeList: [],
		trailerFeeList: [],
		quotation: {},
        customer: {},
		dataSource: {},
		totalDataSource: {}
	},
	reducers: {
        /**
         * 刷新state
         *
         */
		refreshState(state, { payload }) {
			return { ...state, ...payload };
		}
	},
	effects: {
        /**
         * 分页查询报价
         *
         * @param call
         * @param put
         */
		*getQuotationListForPage({ payload }, { call, put }) {
			const res = yield call(getQuotationListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				yield put({
					type: 'refreshState',
					payload: {
						list: res.data.content.list,
						page: res.data.content.page
					}
				});
			} else {
				if (payload.onFail) payload.onFail();
			}
		},
		/**
         * 获取报价详情
         *
         * @param payload
         * @param call
         * @param put
         */
		*getQuotationDetail({ payload }, { call, put }) {
			if (payload.freightId) {
				const res = yield call(getQuotationDetail, { ...R.omit(['onSuccess', 'onFail'])(payload) });
				if (res && res.data.resultCode === "ok") {
					yield put({
						type: 'refreshState',
						payload: {
							dataSource: res.data.content
						}
					});
					if (payload.onSuccess) payload.onSuccess();
				} else {
					if (payload.onFail) payload.onFail();
				}
			} else {
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
			}
		},

		/**
         * 保存报价
		 * 
         * @param payload
         * @param call
         * @param put
         * @param select
         */
		*addQuotation({ payload }, { call, put, select }) {
			let quotationForm = { ...payload };
			const res = yield call(addQuotation, { ...R.omit(['onSuccess', 'onFail'])(quotationForm) });
			if (res && res.data.resultCode === "ok") {
				message.info("报价成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

		/**
         * 更新报价
		 * 
         * @param payload
         * @param call
         * @param put
         * @param select
         */
		*updateQuotationv({ payload }, { call, put, select }) {
			let quotationForm = { ...payload };
			const res = yield call(updateQuotationv, { ...R.omit(['onSuccess', 'onFail'])(quotationForm) });
			if (res && res.data.resultCode === "ok") {
				message.info("更新报价成功！");
				if (payload.onSuccess) payload.onSuccess();
			} else {
				if (payload.onFail) payload.onFail();
			}
		},
		
		/**
         * 计算报价总金额
         *
         * @param call
         * @param put
         */
		*getTotalAmoumt({ payload }, { call, put }) {
			const res = yield call(getTotalAmoumt, { ...R.omit(['onSuccess', 'onFail'])(payload) });
			if (res && res.data.resultCode === "ok") {
				yield put({
					type: 'refreshState',
					payload: {
						totalDataSource: res.data.content
					}
				});
				if (payload.onSuccess) payload.onSuccess(res.data.content);
			} else {
				if (payload.onFail) payload.onFail();
			}
		},

		/**
         * 查询本地费用明细
         * 
         * @param call
         * @param put
         * 
         */
		*getLocalFeeDetail({ payload }, { call, put }) {
			if (payload.freightId) {
				const res = yield call(getLocalFeeDetail, { ...R.omit(['onSuccess', 'onFail'])(payload) });
				if (res && res.data.resultCode === "ok") {
					yield put({
						type: 'refreshState',
						payload: {
							dataSource: res.data.content,
							localFeeList: res.data.content.localFeeList ? res.data.content.localFeeList : [],
							declareFeeList: res.data.content.declareFeeList ? res.data.content.declareFeeList : [],
							trailerFeeList: res.data.content.trailerFeeList ? res.data.content.trailerFeeList : []
						}
					});
				} else {
					if (payload.onFail) payload.onFail();
				}
			} else {
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
			}
		},

		/**
         * 根据ID查询报价记录详情
         *
         * @param id
         */* getRecordById({payload}, {call, put}) {
            const res = yield call(getRecordById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
						dataSource: res.data.content,
						quotation: res.data.content.quotation,
						customer: res.data.content.customer,
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

		/**
		 * 
		 * 
		 * 清空费用list和费用总计
		 */
		*cleanData({}, { call, put }) {
			yield put({
				type: 'refreshState',
				payload: {
					dataSource: {},
					totalDataSource: {}
				}
			});
		}
	},
	subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};
